package com.openvid.webrtc.openvid;

import com.openvid.webrtc.openvid.decoder.FilesDecoder;
import com.openvid.webrtc.openvid.pusher.FileVideoCapturer;

import org.webrtc.AudioSource;
import org.webrtc.AudioTrack;
import org.webrtc.MediaConstraints;
import org.webrtc.MediaStream;
import org.webrtc.PeerConnection;
import org.webrtc.PeerConnectionFactory;
import org.webrtc.RtpSender;
import org.webrtc.VideoSource;
import org.webrtc.VideoTrack;
import org.webrtc.audio.JavaAudioDeviceModule;
import org.webrtc.voiceengine.WebRtcAudioRecord;

import java.io.File;
import java.nio.ByteBuffer;

//https://test-live-xy.deepblueai.com/
public class LocalFileParticipan extends Participant {

    private FileVideoCapturer mVideoCapturer;

    private FilesDecoder mFilesDecoder;
    private VideoTrack mVideoTrack;
    private AudioTrack mAudioTrack;

//    private Collection<IceCandidate> localIceCandidates;
//    private SessionDescription localSessionDescription;

//    public LocalFileParticipan(String participantName, PeerConnectionFactory factory) {
//        super(participantName);
////        localIceCandidates = new ArrayList<>();
////        session.setLocalParticipant(this);
//        initPeerConnectionFactory(factory);
//    }

    private Session mSession;

    public LocalFileParticipan(String participantName, Session s) {
        super(participantName);
//        localIceCandidates = new ArrayList<>();
//        session.setLocalParticipant(this);
        mSession=s;
        initPeerConnectionFactory();

    }


    private JavaAudioDeviceModule mAudioCapture;

    private void initPeerConnectionFactory() {
        PeerConnectionFactory factory=mSession.getPeerConnectionFactory();
        mAudioCapture=mSession.getAudioDeviceModule();

        // Create AudioSource
        AudioSource audioSource = factory.createAudioSource(new MediaConstraints());
        mAudioTrack = factory.createAudioTrack("101", audioSource);


//        PeerConnection p =mSession.createLocalPeerConnection();
//        p.addStream(new MediaStream());
//        RtpSender s=p.addTrack(mAudioTrack);
//        s.setTrack()

        // Create VideoCapturer
        mVideoCapturer = new FileVideoCapturer();
        VideoSource videoSource = factory.createVideoSource(mVideoCapturer.isScreencast());
        mVideoTrack = factory.createVideoTrack("100", videoSource);
        // Display in localView
//        videoTrack.addSink(localVideoView);
        mVideoCapturer.initialize(null, null, videoSource.getCapturerObserver());

    }

    public void play(String path) {

        if (mVideoCapturer == null) {
            return;
        }

        mFilesDecoder = new FilesDecoder();
//        filesDecoder.addDirectory(new File(path),".mp4");
        mFilesDecoder.addFile(new File(path));
        mVideoCapturer.startCapture(0, 0, 0);
        mFilesDecoder.start(new FilesDecoder.IDecoderData() {
            @Override
            public void onVideoFrame(ByteBuffer buffer, int width, int height, long ts) {
                mVideoCapturer.addData(buffer, width, height, ts);
            }

            @Override
            public void onAudioFrame(byte[] f, long ts) {
                mAudioCapture.addPcmData(f);
            }
        });
    }

    @Override
    public void dispose() {
        super.dispose();
        if (mFilesDecoder!=null){
            mFilesDecoder.stop();
            mFilesDecoder=null;
        }
        if (mVideoTrack != null) {
            mVideoTrack.dispose();
            mVideoTrack = null;
        }
        if (mAudioTrack != null) {
            mAudioTrack.dispose();
            mAudioTrack = null;
        }
    }

    @Override
    public AudioTrack getAudioTrack() {
        return mAudioTrack;
    }

    @Override
    public VideoTrack getVideoTrack() {
        return mVideoTrack;
    }
}
